package com.ws.hw_student.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ws.hw_student.system.entity.SysRole;
import com.ws.hw_student.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/sysRole")
@Api(tags = "角色模块")
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;

    // 分页查询角色列表（已有）
    @GetMapping("/pageRole")
    @ApiOperation("分页查询角色列表")
    public Page<SysRole> pageRole(@RequestParam(required = false) String roleName,
                                  @RequestParam(required = false, defaultValue = "1") Integer current,
                                  @RequestParam(required = false, defaultValue = "10") Integer size) {
        Page<SysRole> page = new Page<>(current, size);
        return sysRoleService.queryRolePage(page, roleName);
    }

    // 新增角色
    @PostMapping
    @ApiOperation("新增角色")
    public boolean addRole(@RequestBody SysRole sysRole) {
        return sysRoleService.save(sysRole);
    }

    // 修改角色
    @PutMapping
    @ApiOperation("修改角色")
    public boolean updateRole(@RequestBody SysRole sysRole) {
        return sysRoleService.updateById(sysRole);
    }

    // 删除角色
    @DeleteMapping("/{roleId}")
    @ApiOperation("删除角色")
    public boolean deleteRole(@PathVariable Long roleId) {
        return sysRoleService.removeById(roleId);
    }

    // 根据ID获取角色详情
    @GetMapping("/{roleId}")
    @ApiOperation("根据ID获取角色详情")
    public SysRole getRoleById(@PathVariable Long roleId) {
        return sysRoleService.getById(roleId);
    }

    // 获取所有角色列表（不分页）
    @GetMapping("/listAll")
    @ApiOperation("获取所有角色列表")
    public List<SysRole> listAllRoles() {
        return sysRoleService.list();
    }

    // 检查角色名称是否存在
    @GetMapping("/checkRoleName")
    @ApiOperation("检查角色名称是否存在")
    public boolean checkRoleName(@RequestParam String roleName) {
        return sysRoleService.lambdaQuery()
                .eq(SysRole::getRoleName, roleName)
                .exists();
    }
}